package com.wandian.texas.demo;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.SelectionKey;
import java.nio.channels.SocketChannel;
import java.util.Iterator;

	/**   
	* @author long
	* @QQ 108232706
	* @version 1.0   
	*/   
	public class ConnectCommand implements ICommand {
	public int execute(NIOServer server,SocketChannel channel,Packet packet){
		int playerId=packet.readInt();
		Iterator<Integer> iters=server.ids.iterator();
		boolean sucessed=false;
		while(iters.hasNext()){
			int id=iters.next();
			if(id==playerId){
				System.out.println("=");
				sucessed=true;
				break;
			}
		}
		if(!sucessed){
			server.ids.add(playerId);
			System.out.println("add "+playerId);
			try {
				channel.register(server.selector,SelectionKey.OP_WRITE);
			} catch (ClosedChannelException e) {
				e.printStackTrace();
			}
		}
		return 2000;
	}
	public void write(){
		
	}
}
